﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LeoK.Common;
using LeoK.TM.Entity;
using Telerik.Windows.Controls;

namespace LeoK.TM
{
    /// <summary>
    /// QuestionManager.xaml 的交互逻辑
    /// </summary>
    public partial class QuestionManager : UserControl
    {
        private List<TM_Sln> _Slns;
        private LeoKDataGrid<TM_Question> _leoKDataGrid;
        private GridViewComboBoxColumn _slnCol;

        public QuestionManager()
        {
            InitializeComponent();

            _leoKDataGrid = new LeoKDataGrid<TM_Question>();
            layout.Children.Add(_leoKDataGrid);

            _leoKDataGrid.BeforeGeneratingColumn = BeforeGeneratingColumn;
            Loaded += (sender, args) =>
            {
                LoadData();
            };

            _leoKDataGrid.BeforeSaveData = BeforeSaveData;
        }

        private void BeforeSaveData(TM_Question question)
        {
            if (question.Status == QuestionStatus.已实现 || question.Status == QuestionStatus.已解决)
            {
                question.FixTime = DateTime.Now;
            }
        }

        private bool BeforeGeneratingColumn(GridViewAutoGeneratingColumnEventArgs e)
        {
            bool result = false;

            if (e.Column.UniqueName == "SID")
            {
                _slnCol = new GridViewComboBoxColumn();
                _slnCol.UniqueName = "SID";
                _slnCol.Header = "所属项目";

                _slnCol.IsComboBoxEditable = false;
                _slnCol.DisplayMemberPath = "Name";
                _slnCol.SelectedValueMemberPath = "ID";
                _slnCol.ItemsSource = _Slns;

                e.Column = _slnCol;
                result = true;
            }

            return result;
        }

        public async void LoadData()
        {
            using (MySqlDB db = new MySqlDB())
            {
                _Slns = await db.QueryObjs<TM_Sln>("SELECT * FROM TM_Sln");
                if (_slnCol != null)
                {
                    _slnCol.ItemsSource = _Slns;
                }
                _leoKDataGrid.LoadData();
            }
        }
        
    }
}
